import { ref } from 'vue'
import { defineStore } from 'pinia'
import type { User } from '@/types/user'

// 创建存储用户数据的仓库
export const useUserStore = defineStore(
  'cp-user',
  () => {
    // 保存用户信息
    const userInfo = ref<User>()

    // 修改用户信息
    const setUserInfo = (info: User) => {
      userInfo.value = info
    }

    // 清空用户信息
    const delUserInfo = () => {
      userInfo.value = undefined
    }
    return {
      userInfo,
      setUserInfo,
      delUserInfo
    }
  },
  {
    persist: true // 开启持久化 默认存储在localStorage
  }
)
